﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using MergeShapeFileLib;

namespace MergeShapeFile
{
    public partial class MergeShapeFile : Form
    {
        
        private List<string> files = new List<string>();
        private delegate void setPos();
        private Thread fThread;
        private Thread getProValueThread;
        private Thread mergeThread;
        private MergeShapeFiles mergeShape;
        private string[] dragFilePath;
        private TextBoxRemind remind = null;

        public MergeShapeFile()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            remind = new TextBoxRemind();
            InitTextBoxRemind();
        }

        private void InitTextBoxRemind()
        {
            remind.InitAutoCompleteCustomSource(this.tbSaveFilePath);
        }
        /// <summary>
        /// 选项按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btMore_Click(object sender, EventArgs e)
        {
            if (this.Height == 451)
            {
                this.Height = 194;
                this.btMore.Text = "更多……";
            }
            else
            {
                this.Height = 451;
                this.btMore.Text = "收起";
            }
        }
        /// <summary>
        /// 开始合并
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btStart_Click(object sender, EventArgs e)
        {
            if ((this.tbFilePath.Text.Length != 0) && (this.tbSaveFilePath.Text.Length != 0))
            {
                if (this.tbSaveFilePath.Text.Trim() != "")
                {
                    remind.Remind(this.tbSaveFilePath.Text.Trim());
                    InitTextBoxRemind();
                }

                this.lbStatus.Text = "正在合并文件……";
                this.proUpdate.Maximum = files.Count - 1;

                mergeThread = new Thread(new ThreadStart(mergeShapeFiles));
                mergeThread.Start();

                fThread = new Thread(new ThreadStart(upDateProgressBar));
                fThread.Start();
                this.btStart.Enabled = false;
            }
            else
            {
                MessageBox.Show("所选文件或文件存放路径不能为空！");
            }
        }

        private void btSearch1_Click(object sender, EventArgs e)
        {
            selectMulteFiles();
        }

        private void addItemsToListview(List<string> filenames)
        {
            foreach (string fn in filenames)
            {
                ListViewItem lvi = new ListViewItem(fn);
                this.lvFiles.Items.Add(lvi);
            }
        }

        private void btsearch3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.tbSaveFilePath.Text = dialog.SelectedPath + "\\result.shp";
            }
        }

        private void btSelectFileNoRecursive_Click(object sender, EventArgs e)
        {
            selectForlderNoRecursive();
        }

        private void btSelectFileRecursive_Click(object sender, EventArgs e)
        {
            selectForlderRecursive();
        }

        private void selectFileNoRecursive(string strFolderPath)
        {
            DirectoryInfo di = new DirectoryInfo(strFolderPath);
            FileSystemInfo[] fsinfo = di.GetFileSystemInfos();
            foreach (FileSystemInfo fs in fsinfo)
            {
                if ((fs is FileInfo) && (fs.Extension == ".shp"))
                {
                    files.Add(fs.FullName);
                }
            }
        }

        private void selectFileRecursive(string strFolderPath)
        {
            DirectoryInfo di = new DirectoryInfo(strFolderPath);
            FileSystemInfo[] fsinfo = di.GetFileSystemInfos();
            foreach (FileSystemInfo fs in fsinfo)
            {
                if ((fs is FileInfo) && (fs.Extension == ".shp"))
                {
                    files.Add(fs.FullName);
                }

                if (fs is DirectoryInfo)
                {
                    selectFileRecursive(fs.FullName);
                }
            } 
        }

        private void upDateProgressBar()
        {
            if (this.InvokeRequired)
            {
                setPos setpos = new setPos(upDateProgressBar);
                this.Invoke(setpos, new object[] { });
            }
            else
            {
                getProValueThread = new Thread(new ThreadStart(getProgressValue));
                getProValueThread.Start();
            }
        }

        private void getProgressValue()
        {
            while (true)
            {
                this.proUpdate.Value = MergeShapeFiles.getProValue();
                this.proLable.Text = (this.proUpdate.Value + 1).ToString() + "/" + (this.proUpdate.Maximum + 1).ToString();
                if (this.proUpdate.Value == this.proUpdate.Maximum)
                {
                    MessageBox.Show("合并完成");
                    this.proUpdate.Value = 0;
                    this.lbStatus.Text = "合并完成！";
                    this.btStart.Enabled = true;
                    break;
                }
                else
                {
                    this.proUpdate.Value = MergeShapeFiles.getProValue();
                    this.proLable.Text = (this.proUpdate.Value + 1).ToString() + "/" + (this.proUpdate.Maximum + 1).ToString();
                }
            }
        }

        private void mergeShapeFiles()
        {
            mergeShape = new MergeShapeFiles(files, this.tbSaveFilePath.Text);
            mergeShape.mergeFiles();
        }
        
        private void btCancle_Click(object sender, EventArgs e)
        {
            
        }

        private void tbFilePath_DragDrop(object sender, DragEventArgs e)
        {
            dragFilePath = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            string filePaths = null;
            for (int i = 0; i < dragFilePath.Length; i++)
                filePaths += dragFilePath[i];
            this.tbFilePath.Text = filePaths;
            foreach (string filePath in dragFilePath)
            {
                files.Add(filePath);
            }
            addItemsToListview(files);
        }

        private void tbFilePath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }

        private void rcSelectFiles_Click(object sender, EventArgs e)
        {
            selectMulteFiles();
        }

        private void selectMulteFiles()
        {
            files.Clear();
            this.lvFiles.Items.Clear();
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "(*.shp)|*.shp";
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string filePath in openFileDialog.FileNames)
                {
                    files.Add(filePath);
                    this.tbFilePath.Text += filePath;
                }
                addItemsToListview(files);
            }
            this.proUpdate.Maximum = files.Count;
        }

        private void rcSelectRecursive_Click(object sender, EventArgs e)
        {
            selectForlderRecursive();
        }

        private void selectForlderRecursive()
        {
            this.lvFiles.Items.Clear();
            files.Clear();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string folderPath = dialog.SelectedPath;
                this.tbFilePath.Text = folderPath;
                selectFileRecursive(folderPath);
                addItemsToListview(files);
            }
            this.proUpdate.Maximum = files.Count;
        }

        private void rcSelectNoRecursive_Click(object sender, EventArgs e)
        {
            selectForlderNoRecursive();
        }

        private void selectForlderNoRecursive()
        {
            this.lvFiles.Items.Clear();
            files.Clear();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string folderPath = dialog.SelectedPath;
                this.tbFilePath.Text = folderPath;
                selectFileNoRecursive(folderPath);
                addItemsToListview(files);
            }
            this.proUpdate.Maximum = files.Count;
        }
    }
}
